package cn.oa.dao.impl;

import cn.oa.dao.BaseDao;
import cn.oa.dao.DepartmentDao;
import cn.oa.entity.Department;
import cn.oa.util.DatabaseUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class DepartmentDaoImpl extends BaseDao implements DepartmentDao {
    public DepartmentDaoImpl(Connection conn) {
        super(conn);
    }

    @Override
    public List<Department> getAllDepartments() throws SQLException {
        List<Department> list = new ArrayList<Department>();
        ResultSet rs = null;
        String sql = "select * from department";
        try{
            rs = this.executeQuery(sql);
            while(rs.next()){
                Department department = new Department();
                department.setId(rs.getInt("id"));
                department.setDeptName(rs.getString("deptName"));
                list.add(department);
            }
        }catch(SQLException e){
            e.printStackTrace();
        }finally {
            DatabaseUtil.closeAll(null, null, rs);
        }
        return list;
    }
}
